/*
 * firings_array.cpp
 *
 *  Created on: 12.04.2012
 *      Author: delgado
 */

#include "firings_array.h"

#include <cstdlib>
using std::rand;
using std::srand;
#include <ctime>
using std::time;
#include <algorithm>
using std::fill;

using std::cout;
using std::endl;

namespace neurignacio{

Pattern_Array::Pattern_Array(const number_t n, const real a) : n(n), a(a)
{
	number_t nactive = static_cast<number_t>(n*a); // number of active neurons
	vector<uint8> Z0(n,0);	// Create array of size n filled with 0
	fill(Z0.begin(), Z0.begin()+nactive, 1);	// Fill the first nactive places with 1
	this->push_back(Z0);	// Append the first pattern to the list (copy by value)
}

void Pattern_Array::generate(const number_t m)
{
//	cout << "Pattern_Array::generate("<< m << ")" << endl;
	// Reset the seed
	srand(time(NULL));
	vector<uint8> Zp(n,0);	// Create array of size n filled with 0
//	cout << "Zp.size()="<< Zp.size() << endl;
	for (number_t p=0; p<m; ++p)
	{
		for (vector<uint8>::iterator i=Zp.begin(); i!=Zp.end(); ++i)
		{
			real random = static_cast<real>(rand()) / RAND_MAX;
			*i=(random < a? 1 : 0);	// Fill Zp with 1
//			cout << random << "\t";
		}
//		cout << endl;
		this->push_back(Zp); // Append Zp to pattern (copy by value)
	}
}

ostream& operator<<(ostream& out, const Pattern_Array& pattern)
{
	number_t counter=0;
	for (Pattern_Array::const_iterator p=pattern.begin(); p!=pattern.end(); ++p, ++counter)
	{
			out << "#" << counter << ": [ ";
			for (vector<uint8>::const_iterator i=p->begin(); i!=p->end(); ++i)
				out << (int)*i << " ";
			out << "]" << endl;
		}
	return out;
}

} // end namespace neurignacio


